<template>
  <view class="search-list">
    <!-- 搜索框 -->
    <view class="search-top">
      <view class="search-box" @tap="toSearchPage(1)">
        <view class="icon">
          <image src="/static/images/search.png" />
        </view>
        <div v-if="showKeyword" class="key-tag">
          {{ showKeyword }}<span class="close" @tap.stop="toSearchPage(0)">x</span>
        </div>
        <div v-if="!showKeyword" class="text">
          输入关键字搜索
        </div>
      </view>
    </view>
    <view class="content" v-for="item in Data.list" :key="item.postId" @tap="gotoPostDetail(item.postId)">
      <view class="update-time">
        {{ item.updateTime }}
      </view>
      <view class="card">
        <image v-if="item.files.length > 0" class="img" :src="item.files[0].fileUrl" mode="aspectFit" />
        <image v-else class="img" src="../../static/empty-img/no-img.gif" mode="aspectFit" />
        <view class="header">
          <view class="title text-overflow">
            {{ item.title }}
          </view>
          <view class="status have-not" v-if="judgeTime(item.startTime, item.endTime) == 0">
            未开始
          </view>
          <view class="status start" v-else-if="judgeTime(item.startTime, item.endTime) == 1">
            进行中
          </view>
          <view class="status end" v-else>
            已结束
          </view>
        </view>
        <view class="body">
          <view class="time text-overflow">{{ item.startTime }}</view>
          <view class="location text-overflow">{{ item.location || '暂无地址' }}</view>
        </view>
      </view>
    </view>
    <view v-if="isLoadAll && Data.list.length > 0" class="nomore">
      没有更多了，看看别的吧
    </view>
    <empty-img v-if="isEmpty">
      <template #img>
        <image src="../../static/empty-img/not-found.png" mode="aspectFit"></image>
      </template>
      <template #text>
        没有找到对应商品，看看别的吧
      </template>
    </empty-img>
  </view>
</template>

<script setup>
import {
  ref
} from 'vue';
const showKeyword = ref('')
onLoad((options) => {
  showKeyword.value = options.keyword
  queryData.value.title = options.keyword
  getList() //获取帖子列表
})
/**
 * to-搜索页
 * @param type 0:删除关键词跳转 1:直接跳转
 */
const toSearchPage = (type) => {
  const url = type === 0 ? '/pages/search-page-index/search-page-index' :
    '/pages/search-page-index/search-page-index?keyword=' + showKeyword.value
  uni.redirectTo({
    url
  })
}
/**
 * 查询条件
 */
const isEmpty = ref(false)
const Data = ref({})
const queryData = ref({
  pageSize: 10,
  pageNum: 1,
  columns: "updateTime",
  orders: "DESC",
  title: "",
  sectionId: "",
  userId: "",
  sysType: "2",
})
const getList = () => {
  uni.showLoading()
  http.request({
    url: '/mall4cloud_point/ua/post/page',
    method: 'GET',
    data: queryData.value
  }).then((res) => {
    if (res.total === 0) {
      isEmpty.value = true
    }
    if (queryData.value.pageNum === 1) {
      Data.value = res
    } else {
      let list = Data.value.list
      list.push(...res.list)
      Data.value.list = list
    }
    if (queryData.value.pageNum === Data.value.pages) {
      isLoadAll.value = true
    }
    uni.hideLoading()
  })
}
/**
 * 页面上拉触底事件的处理函数
 */
const isLoadAll = ref(false)
onReachBottom(() => {
  if (queryData.value.pageNum < Data.value.pages) {
    queryData.value.pageNum = queryData.value.pageNum + 1
    getList()
  } else {
    isLoadAll.value = true
  }
})
/**
 * 根据活动时间判断是否未开始，进行中，已结束
 */
const judgeTime = (start, end) => {
  const startTime = new Date(start.replace(/\-/g, '/'));
  const endTime = new Date(end.replace(/\-/g, '/'));
  const currentTime = new Date();
  if (currentTime < startTime) {
    return 0
  } else if (currentTime >= startTime && currentTime <= endTime) {
    return 1
  } else {
    return 2
  }
}
  /**
   * 去帖子详情
   */
   const gotoPostDetail = (id) => {
    uni.navigateTo({
      url: '/pages/activity-detail/activity-detail?id=' + id
    })
  }
</script>

<style lang="scss" scoped>
@use "search-list-index";
</style>
